package cn.com.gridinfo.sooc.core.expand.callname.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import org.apache.http.message.BasicNameValuePair;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.activity.QuexiClassActivity;
import cn.com.gridinfo.sooc.core.expand.callname.activity.QuexiTongjiActivity;
import cn.com.gridinfo.sooc.core.expand.callname.bean.Student;
import cn.com.gridinfo.sooc.utils.IntentUtil;

/**
 * 缺席统计界面列表适配器
 *
 * @author yingchunli
 */
public class QueXiTongAdapter extends UltimateViewAdapter<QueXiTongAdapter.ViewHolder> {

    public List<Student> studentList;
    Context context;
    private String kcid;//课程id
    private String kcmc;//课程名称

    public QueXiTongAdapter(List<Student> studentList, Context context, String kcid, String kcmc) {
        this.studentList = studentList;
        this.context = context;
        this.kcid = kcid;
        this.kcmc = kcmc;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_callname_quexitong_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        view.setOnClickListener(holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= studentList.size() : position < studentList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            Student student = studentList.get(position);
            holder.xm.setText(student.getXm() + "(" + student.getXh() + ")");
            holder.bj.setText(student.getBj());
            holder.qxcs.setText(student.getQxcs());
            //底部分割线的显示
//            if (position == studentList.size() - 1) {
//                holder.line.setVisibility(View.VISIBLE);
//            } else {
//                holder.line.setVisibility(View.GONE);
//            }
        }
    }

    public void loadmore(List<Student> list) {
        studentList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<Student> list) {
        studentList.clear();
        studentList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getAdapterItemCount() {
        return studentList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder implements View.OnClickListener {
        TextView xm, bj, qxcs;
//        View line;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                xm = (TextView) itemView.findViewById(R.id.xm_quexitongji_item);//姓名
                bj = (TextView) itemView.findViewById(R.id.banji_quexitongji_item);//班级
                qxcs = (TextView) itemView.findViewById(R.id.count_quexitongji_item);//缺席次数
//                line = itemView.findViewById(R.id.line_bottom);//底部分割线
            }
        }

        @Override
        public void onClick(View v) {
            //跳转到缺席课堂界面  带参数学号和课程id
            Intent intent = new Intent(context, QuexiClassActivity.class);
            intent.putExtra("xh", studentList.get(getAdapterPosition()).getXh());
            intent.putExtra("xm", studentList.get(getAdapterPosition()).getXm());
            intent.putExtra("bj", studentList.get(getAdapterPosition()).getBj());
            intent.putExtra("qxcs", studentList.get(getAdapterPosition()).getQxcs());
            intent.putExtra("kcid", kcid);
            intent.putExtra("kcmc", kcmc);
            context.startActivity(intent);
            AnimUtil.intentSlidIn((Activity) context);
        }
    }
}
